package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.dto.GroupNiceDTO;
import com.yami.shop.bean.dto.SetUpGroupAdminDTO;
import com.yami.shop.bean.model.MeltCloudGroup;
import com.yami.shop.bean.model.MeltCloudGroupUser;
import com.yami.shop.bean.model.MeltCloudUser;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.common.bean.PageDTO;
import com.yami.shop.common.bean.PageVO;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.dto.user.GroupAnnouncementDTO;
import com.yami.shop.service.MeltCloudGroupService;
import com.yami.shop.service.MeltCloudGroupUserService;
import com.yami.shop.service.MeltCloudUserService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.impl.RongYunAllMembersServiceImpl;
import com.yami.shop.service.impl.RongYunGagServiceImpl;
import com.yami.shop.service.impl.RongYunGroupServiceImpl;
import com.yami.shop.vo.group.GroupModelSyncVo;
import com.yami.shop.vo.group.GroupVo;
import com.yami.shop.vo.group.gag.GroupGagVo;
import com.yami.shop.vo.group.gag.GroupGayRemVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/melt/group")
@Tag(name = "融云群组控制")
public class MeltCloudGroupController {

    @Autowired
    private MeltCloudGroupService groupService;

    @Autowired
    private MeltCloudGroupUserService groupUserService;
    @Autowired
    private MeltCloudUserService meltCloudUserService;

    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private RongYunGroupServiceImpl rongYunGroupService;

    @Autowired
    private RongYunGagServiceImpl gagService;
    @Autowired
    private RongYunAllMembersServiceImpl muteAllMembersService;

    @GetMapping("/list")
    @Operation(summary =  "群组列表", description = "群组列表")
    public ServerResponseEntity<PageVO<MeltCloudGroup>> page(PageDTO pageDTO, MeltCloudGroup param) {
        PageVO<MeltCloudGroup> vo = groupService.getAll(pageDTO, param);
        vo.getList().forEach(meltCloudGroup -> {
            MeltCloudUser byId = meltCloudUserService.getById(meltCloudGroup.getCreatorUserId());
            meltCloudGroup.setCreatorUserName(byId.getUserName());
            if (Objects.nonNull(meltCloudGroup.getShopId()) && meltCloudGroup.getShopId() != 0) {
                ShopDetail shop = shopDetailService.getShopDetailByShopId(meltCloudGroup.getShopId());
                meltCloudGroup.setShopName(shop.getShopName());
            }
        });
        return ServerResponseEntity.success(vo);
    }


    @PostMapping("/createGroup")
    @Operation(summary =  "创建群组")
    public ServerResponseEntity<MeltCloudGroup> create(@RequestBody @Valid GroupVo groupVo) throws Exception {
        return rongYunGroupService.create(groupVo);
    }

    @PostMapping("/updateGroup")
    @Operation(summary =  "修改群名称", description = "修改群名称")
    public ServerResponseEntity<String> updateGroupName(@RequestBody GroupModelSyncVo groupModelSyncVo) throws Exception {
        return rongYunGroupService.update(groupModelSyncVo);
    }

    @PostMapping("/updateUserNick")
    @Operation(summary =  "设置群成员昵称", description = "设置群成员昵称")
    public ServerResponseEntity<String> updateUserGroupNike(@RequestBody GroupNiceDTO groupModelSyncVo) {
        return rongYunGroupService.updateUserGroupNike(groupModelSyncVo);
    }
    @PostMapping("/setUpAdminTaboo")
    @Operation(summary =  "设置管理员")
    public ServerResponseEntity<String> setUpAdminTaboo(@RequestBody @Valid SetUpGroupAdminDTO setUpGroupAdminDTO) throws Exception {
        return rongYunGroupService.setUpAdmin(setUpGroupAdminDTO.getUserId(),setUpGroupAdminDTO.getGroupId(), setUpGroupAdminDTO.getCurrentUserId());
    }

    @PostMapping("/cancelPlatAdmin")
    @Operation(summary =  "取消群管理员")
    public ServerResponseEntity<String> cancelPlatAdmin(@RequestBody SetUpGroupAdminDTO setUpGroupAdminDTO){
        return rongYunGroupService.cancelAdmin(setUpGroupAdminDTO.getUserId(),setUpGroupAdminDTO.getGroupId(), setUpGroupAdminDTO.getCurrentUserId());
    }

    @PostMapping("/publishAnnouncement")
    @Operation(summary =  "发布群公告", description = "发布群公告")
    public ServerResponseEntity<String> publishAnnouncement(@RequestBody GroupAnnouncementDTO updateGroupAnnouncement) throws Exception {
        return rongYunGroupService.publishAnnouncement(updateGroupAnnouncement);
    }

    @GetMapping("/getGroupUsers")
    @Operation(summary =  "获取群成员", description = "获取群成员")
    public ServerResponseEntity<PageVO<MeltCloudUser>> getInfo(PageDTO pageDTO, @Param("groupId") String groupId, @Param("userName") String userName,@Param("status")String status) throws Exception {
        List<MeltCloudGroupUser> list = groupUserService.list(new LambdaQueryWrapper<MeltCloudGroupUser>().eq(MeltCloudGroupUser::getGroupId, groupId).eq(MeltCloudGroupUser::getIsDel,0));
        List<String> users = list.stream().map(MeltCloudGroupUser::getUserId).collect(Collectors.toList());
        PageVO<MeltCloudUser> vo = meltCloudUserService.getAll(pageDTO, userName, groupId, users,status);
        return ServerResponseEntity.success(vo);
    }

    @PostMapping("/dismiss")
    @Operation(summary =  "解散群组", description = "解散群组")
    public ServerResponseEntity<String> dismiss(@RequestBody GroupVo groupVo) throws Exception {
        return rongYunGroupService.dismiss(groupVo);
    }

    @PostMapping("/taboo")
    @Operation(summary =  "禁言单个或多个用户")
    public ServerResponseEntity<String> create(@RequestBody @Valid GroupGagVo groupVo) throws Exception {
        return gagService.add(groupVo);
    }

    @PostMapping("/cancelTaboo")
    @Operation(summary =  "移除禁言单个或多个用户")
    public ServerResponseEntity<String> remove(@RequestBody @Valid GroupGayRemVo groupVo) throws Exception {
        return gagService.remove(groupVo);
    }

    @PostMapping("/groupMute")
    @Operation(summary =  "添加群禁言")
    public ServerResponseEntity<String> muteAllMembersAdd(@RequestBody List<String> groupIds) throws Exception {
        return muteAllMembersService.muteAllMembersAdd(groupIds);
    }

    @PostMapping("/groupMuteCancel")
    @Operation(summary =  "移除群禁言")
    public ServerResponseEntity<String> muteAllMembersRemove(@RequestBody List<String> groupIds) throws Exception {
        return muteAllMembersService.muteAllMembersRemove(groupIds);
    }

    @PostMapping("/group_quit")
    @Operation(summary =  "退出群组")
    public ServerResponseEntity<String> quit(@RequestBody @Valid GroupVo groupVo) throws Exception {
        return rongYunGroupService.quit(groupVo);
    }
}
